package service

import (
	"context"
	pb "demo_grpc/proto"
	"fmt"
	"strconv"
)

// 定义server，用来实现proto文件，里面实现的Greeter服务里面的接口
type Server struct {
	pb.UnimplementedGreeterServer
}

// 实现SayHello接口
func (s *Server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	// 创建一个HelloReply消息，设置Message字段，然后直接返回。
	var maxAge = 10

	name := in.Name
	age := in.Age
	var ageInt int
	var err error
	ageInt, err = strconv.Atoi(age)
	var message string
	var event string
	if ageInt > maxAge {
		message = fmt.Sprintf("%s超过了最大年纪限制，%d", name, maxAge)
		event = "超限"
	} else {
		message = fmt.Sprintf("%s符合年纪限制", name)
		event = "符合规定"
	}

	return &pb.HelloReply{Message: message, Event: event}, err
}
